package com.dearbinge.opentapi.content.ShowField;

import com.dearbinge.data.api.ShowField.ShowFieldService;
import com.dearbinge.data.pojo.showField.ShowField;
import com.dearbinge.utils.PageResult;
import com.dearbinge.utils.ResultMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.List;
import java.util.Map;


@Api(description = "秀场")
@RestController
@RequestMapping("/showField")
@EnableAutoConfiguration
public class ShowFieldController extends WebMvcConfigurerAdapter {

    @Autowired
    private ShowFieldService showFieldService;


    /**
     * 新增/修改
     */
    @ApiOperation(value = "新增/修改",notes = "新增/修改")
    @RequestMapping(value ="/addOrUpdate",method = RequestMethod.POST)
    public ResultMsg<String> addOrUpdate(@RequestBody ShowField showField){
        try {
            if (showField.getId() != null && showField.getId() != 0){
                Integer dataCode = showFieldService.update(showField);
                if (dataCode > 0){
                    return ResultMsg.ok("修改成功");
                }
            }else {
                Integer dataCode = showFieldService.insert(showField);
                if (dataCode > 0){
                    return ResultMsg.ok("新增成功");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 根据ID查询秀场
     */
    @ApiOperation(value = "根据ID查询",notes = "根据ID查询")
    @ApiImplicitParam(name = "id",value = "id",paramType = "query")
    @RequestMapping(value = "/getById",method = RequestMethod.GET)
    public ResultMsg<Map<String,Object>> getById(Integer id){
        try {
            Map<String,Object> showField = showFieldService.getById(id);
            return ResultMsg.ok(showField);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 根据ID删除
     */
    @ApiOperation(value = "根据ID删除",notes = "根据ID删除")
    @ApiImplicitParam(name = "id",value = "id",paramType = "query")
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public ResultMsg<String> delete(Integer id){
        try {
            Integer dataCode = showFieldService.delete(id);
            if (dataCode > 0){
                return ResultMsg.ok("删除成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 根据经纬度查询附件（人/商户），使用充值排名度排序
     */
    @ApiOperation(value = "根据经纬度查询附件（人/商户），使用充值排名度排序",notes = "根据经纬度查询附件（人/商户），使用充值排名度排序")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lon",value = "经度",paramType = "query"),
            @ApiImplicitParam(name = "lat",value = "纬度",paramType = "query"),
            @ApiImplicitParam(name = "positionName",value = "区域名称",paramType = "query"),
            @ApiImplicitParam(name = "positionDetailedName",value = "区域详细名称",paramType = "query"),
            @ApiImplicitParam(name = "raidus",value = "范围（米为单位）",paramType = "query"),
            @ApiImplicitParam(name = "commodityId",value = "商品ID",paramType = "query"),
            @ApiImplicitParam(name = "id",value = "返回结果集的第一个的ID",paramType = "query"),
            @ApiImplicitParam(name = "thumbsType",value = "类型(0：个人   1：订单)",paramType = "query"),
            @ApiImplicitParam(name = "userId",value = "当前用户ID",paramType = "query"),
            @ApiImplicitParam(name = "pageNo",value = "当前页",paramType = "query"),
            @ApiImplicitParam(name = "pageSize",value = "页面数量",paramType = "query")})
    @RequestMapping(value = "/getByPage",method = RequestMethod.GET)
    private PageResult<List<Map<String, Object>>> getByPage(Integer pageNo,
                                                            Integer pageSize,
                                                            String positionName,
                                                            String positionDetailedName,
                                                            String userId,
                                                            String commodityId,
                                                            Integer thumbsType,
                                                            Double lon,
                                                            Double lat,
                                                            Integer raidus,
                                                            Integer id){
        PageResult<List<Map<String, Object>>> pageResult = new PageResult<>();
        try {
            Double minLat = null;
            Double maxLat = null;
            Double minLng = null;
            Double maxLng = null;
            Double latitude = lat;
            Double longitude = lon;
            if(raidus != null && raidus != 0 && lon != null && lon != 0 && lat != null && lat != 0 ){
                // 赤道周长24901英里 1609是转换成米的系数
                Double degree = (24901 * 1609) / 360.0;
                double raidusMile = raidus;

                Double dpmLat = 1 / degree;
                Double radiusLat = dpmLat * raidusMile;
                 minLat = latitude - radiusLat;
                 maxLat = latitude + radiusLat;

                Double mpdLng = degree * Math.cos(latitude * (Math.PI / 180));
                Double dpmLng = 1 / mpdLng;
                Double radiusLng = dpmLng * raidusMile;
                 minLng = longitude - radiusLng;
                 maxLng = longitude + radiusLng;
            }

            pageResult = showFieldService.getByPage(minLat,minLng,maxLat,maxLng,userId,commodityId,thumbsType,longitude,latitude,pageNo,pageSize,positionName,positionDetailedName,id);
        }catch (Exception e){
            e.printStackTrace();
        }
        return pageResult;
    }



}
